#include "XmlTag.h"
//#include "../utils/StringPatternMatcher.h"

using namespace kc_sf;

// Konstruktory ----------------------------------------------------- //

Tag::Tag( const std::string &_name, const std::string &_value,
          Type _type)
: t_name( _name ), t_value( _value ), t_type( _type ) 
{
}

// ------------------------------------------------------------------ //

void Tag::setName( const std::string &name )
{
    t_name = name;
}

void Tag::setValue( const std::string &value )
{
    t_value = value;
}

void Tag::setType( Type type )
{
    t_type = type;
}

const std::string & Tag::name() const
{
    return t_name;
}

const std::string &  Tag::value() const
{
    return t_value;
}

Tag::Type Tag::type() const
{
    return t_type;
}

// ------------------------------------------------------------------ //

std::string Tag::attribute( const std::string &attr )
{
    std::string ret;

    if( attr.empty() )
        return ret;
    
    unsigned int start;
    StringPatternMatcher matcher( t_name, attr + "=" );
    
    if( matcher.match( start, 0 ) )
    {
        start += attr.size() + 1;    // ="

        if( start < t_name.size() - 1 && t_name[start] == '\"' )
        {
            ++ start;   // bez "
            unsigned int end = t_name.find( "\"", start );
            if( end < t_name.size() )
            {
                ret = t_name.substr( start, end - start );
            }
        }
    }
    
    return ret;
}
